package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.advice.LogOperation;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Emp;
import com.itheima.service.DeptService;
import com.itheima.utils.TliasException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper,Dept> implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;
    @Override
    public List<Dept> findAll() {
//        return deptMapper.selectList(null);
        return list();
    }
    @Override
    public void deleteById(Integer id) {
        LambdaQueryWrapper<Emp> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(Emp::getName).eq(Emp::getDeptId,id);
        Long count = empMapper.selectCount(queryWrapper);
        if(count > 0){
            throw new TliasException("部门下有员工， 不能删除");
        }
        deptMapper.deleteById(id);
        removeById(id);

    }

    @Override
    public void saveDept(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
//        deptMapper.insert(dept);
        save(dept);
    }

    @Override
    public Dept foundById(Integer id) {
//        Dept dept =deptMapper.selectById(id);
        Dept dept =getById(id);
        return dept;
    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
//        deptMapper.updateById(dept);
        updateById(dept);
    }


//    @Override
//    public void saveDept(Dept dept){
//        dept.setCreateTime(LocalDateTime.now());
//        dept.setUpdateTime(LocalDateTime.now());
//        deptMapper.saveDept(dept);
//    }
}
